package coc.core;

import java.io.InputStream;
import java.util.concurrent.FutureTask;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import coc.core.CrawlerConfig;
import coc.core.Spider;
import coc.core.SpiderResult;
import coc.core.collector.CollectorType;
import coc.core.url.Url;
import coo.base.util.FileUtils;

public class SpiderTestForHtmlUnit {
	private Logger log = LoggerFactory.getLogger(getClass());

	@Test
	public void test() throws Exception {
		Url url = new Url(
				"http://epaper.oeeee.com/A/html/2013-04/10/content_1837108.htm");

		CrawlerConfig config = new CrawlerConfig();
		config.setStartUrl(url);
		config.setScript(getScript());
		config.setCollectorType(CollectorType.HTMLUNIT);
		Spider spider = new Spider(url, config);
		FutureTask<SpiderResult> futureTask = new FutureTask<SpiderResult>(
				spider);
		futureTask.run();

		log.debug(futureTask.get().getData().toString());
	}

	private String getScript() {
		StringBuilder builder = new StringBuilder();
		builder.append("this.interpreter.set(\"$text\", \".wqbcontentcss\");");
		builder.append("this.interpreter.set(\"$author\", \".font_12_666666_h26_center>a\");");
		builder.append("this.interpreter.set(\"$title\", \".font_26_333333_h40_center\");");
		builder.append("this.interpreter.set(\"$publishDate\", \".font_12_666666_h26_center\");");

		InputStream in = getClass().getResourceAsStream("SpiderTest.bsh");
		return builder.append(new String(FileUtils.toByteArray(in))).toString();
	}
}
